package com.blog.controller.manage;

import com.blog.bean.User;
import com.blog.constant.RoleConstant;
import com.blog.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/adminManage")
public class AdminManageController {

    @Resource
    private UserService userService;

    /**
     * 新增管理员
     *
     * @param user
     * @return
     */
    @RequestMapping("/addAdmin")
    @RequiresRoles(RoleConstant.ROLE_ADMINISTRATOR)
    @ResponseBody
    public Object addAdmin(User user) {
        Map<Object, Object> map = new HashMap<>();
        Md5Hash newPasswordMd5 = new Md5Hash(user.getUserPassword(), user.getUserName(), 1024);
        user.setUserPassword(newPasswordMd5.toString());
        int count = userService.insertSelective(user);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 判断用户名是否被使用
     *
     * @param userName
     * @return
     */
    @RequestMapping("/checkUserName")
    @RequiresRoles(RoleConstant.ROLE_ADMINISTRATOR)
    @ResponseBody
    public Object checkUserName(@RequestParam(value = "userName") String userName) {
        Map<Object, Object> map = new HashMap<>();
        User u = new User();
        u.setUserName(userName);
        User user = userService.getUser(u);
        if (user != null) {
            map.put("result", "已经存在");
        }
        return map;
    }

    /**
     * 删除管理员
     *
     * @param userId
     * @return
     */
    @RequestMapping("/deleteAdmin")
    @RequiresRoles(RoleConstant.ROLE_ADMINISTRATOR)
    @ResponseBody
    public Object deleteAdmin(@RequestParam(value = "userId") Long userId) {
        Map<Object, Object> map = new HashMap<>();
        int count = userService.deleteByPrimaryKey(userId);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 更新管理员
     * 更新权限，更新密码
     *
     * @param user
     * @return
     */
    @RequestMapping("/updateAdmin")
    @RequiresRoles(RoleConstant.ROLE_ADMINISTRATOR)
    @ResponseBody
    public Object updateAdmin(User user) {
        Map<Object, Object> map = new HashMap<>();
        if (user.getUserPassword() != null) {
            Md5Hash newPasswordMd5 = new Md5Hash(user.getUserPassword(), user.getUserName(), 1024);
            user.setUserPassword(newPasswordMd5.toString());
        }
        int count = userService.updateByPrimaryKeySelective(user);
        if (count > 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 获得管理员
     *
     * @param userId
     * @return
     */
    @RequestMapping("/getAdmin")
    @RequiresRoles(RoleConstant.ROLE_ADMINISTRATOR)
    @ResponseBody
    public Object getAdmin(@RequestParam(value = "userId") Long userId) {
        Map<Object, Object> map = new HashMap<>();
        User u = new User();
        u.setUserId(userId);
        User user = userService.getUser(u);
        if (user != null) {
            map.put("user", user);
        }
        return map;
    }

    /**
     * 管理员列表
     *
     * @param pn
     * @param model
     * @return
     */
    @RequestMapping("/adminList")
    @RequiresRoles(RoleConstant.ROLE_ADMINISTRATOR)
    public String adminList(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 15);
        List<User> userList = userService.selectUser(new User());
        List<User> adminList = new ArrayList<>();
        for (User temp : userList) {
            // 排除普通用户和超管，阻止横向越权
            if (temp.getUserLevelId() != null && temp.getUserLevelId() != RoleConstant.LEVEL_ADMINISTRATOR) {
                adminList.add(temp);
            }
        }
        PageInfo<User> pageInfo = new PageInfo<>(adminList);
        model.addAttribute("pageInfo", pageInfo);
        return "manage-admin";
    }
}
